define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var chioceTab = require("chioceTab");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");
	var commonBack = require("commonBack");
	//钉钉返回
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	var dingTalkNav = require('dd_config');
	//选择供应商
	var newAdd = require("../chioceProduct/newAdd.js");
	newAdd.supplierChioce(".supplierChioce");
	require("../chioceProduct/init.js");
	var muiTips = require("muiTips"); //封装的弹框
	var keyWordText = ""; //关键字搜索
	$("#startStatusId").attr("data-id", abp.infoStatusId.ENABLE_STATUS);
	$("#banStatusId").attr("data-id", abp.infoStatusId.DISABLE_STATUS);
	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityIdList = authorityInit.getJurisdictionId(authorityData); //权限一级判断
	var authoritySupplier = authorityInit.getJurisdiction(authorityData, sourceType.INFO_SUPPLIER); //供应商权限判断
	function getRightFun() {
		document.location.href = "../../src/chioceSupplier/newSupplier.html?dd_nav_bgcolor=FFFF943E";
	}
	if(authorityIdList.indexOf(sourceType.INFO_SUPPLIER) === -1) {
		$('.mui-content').addClass('initContentTips');
		$('.mui-content > .tipsConInit').html("您没有权限查看供应商资料哦!");
		$("header > a").remove();
		dingTalkNav.RightNavBar(false, "新增", "default");
	} else {
		if(authoritySupplier.indexOf(2) === -1) {
			$("header > a").remove();
			dingTalkNav.RightNavBar(false, "新增", "default");
		} else {
			$("header > a").show();
			dingTalkNav.RightNavBar(true, "新增", getRightFun);
		}
		if(authoritySupplier.indexOf(1) === -1) {
			$('.mui-content > .tipsConInit').html("您没有权限查看供应商资料哦!");
			$('.mui-content').addClass("initContentTips");
		} else {
			var getSearchItem = require("getSearchItem"); //调用封装搜索
			getSearchItem.getSearchItem("#chioce-search"); //添加搜索效果
			getSearchItem.noSearchTap("ul.pop-tab .pop-tab-titleitem"); //点击其他遮罩层关闭搜索
			$("body").on("keyup", "#searchText", function() { //搜索框事件
				keyWordText = $("#searchText").val();
				searchSupplier("outSupplierList", "#mui-list", "searchContentTips", false);
			});

			var pageIndex = 1; //当前分页
			var dataState = true; //判断如何渲染页面，true替换加载，false加载在末尾
			var pageState = 10; //判断有多少页，调用接口再动态改
			mui.init({
				swipeBack: true, //启用右滑关闭功能
				pullRefresh: { //配置分页下拉刷新
					container: '#pullrefresh',
					down: {
						callback: pulldownRefresh
					},
					up: {
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
			});

			/*渲染客户*/
			searchSupplier("outSupplierList", "#mui-list", "initContentTips", true);
			/*分类请求*/
			var infoCompanyCatalogId, orderBy;
			var statusId = [];
			/*获取分类Id*/
			mui("body").on("tap", "#chioceSupplierCon>li", function() {
				infoCompanyCatalogId = Number($(this).attr("data-id"));
				console.log(infoCompanyCatalogId);
				dataState = true;
			});
			/*获取状态码Id*/
			mui("body").on("tap", "#statusId>li", function() {
				statusId[0] = Number($(this).children("a").attr("data-id"));
			});
			/*重新渲染页面*/
			mui("body").on("tap", "#mui-sure", function() {
				pageIndex = 1;
				searchSupplier("outSupplierList", "#mui-list", "searchContentTips", false);
				chioceTab.muiSure();
			});
			//加载数据
			mui("body").on("tap", ".pop-tab-con2 li", function() {
				pageIndex = 1;
				dataState = true;
				var catalogOrderBy = "";
				catalogOrderBy = $(this).attr('data-orderBy');
				orderBy = "DataModified " + catalogOrderBy;
				searchSupplier("outSupplierList", "#mui-list", "searchContentTips", false);
				chioceTab.muiSure();
				//	    console.log(orderBy)
			});

			$('body').one('tap', '.pop-tab .pop-tab-title1', function() {
				var getSuppliertList = require("./supplierType.js");
				getSuppliertList.supplier("chioceSupplier", "#chioceSupplierCon");
			});
			/*获取客户Id*/
			var supplierId;
			mui("body").on("tap", "#mui-list>li", function() {
				supplierId = Number($(this).attr("data-id"));
				// 		alert(1)
				//     		console.log(supplierId)
				sessionStorage.setItem("supplierId", supplierId);
			});

		}
	}

	function searchSupplier(templateId, mainId, catalogInit, isChioce) {
		var option = {};
		if(!orderBy) {
			orderBy = "";
		}
		var dataModel = {
			"keyword": keyWordText,
			"areaID": 0,
			"infoCompanyCatalogId": infoCompanyCatalogId,
			"orderBy": orderBy,
			"statusId": statusId,
			"pageIndex": pageIndex,
			"pageSize": 20
		};
		abp.services.app.supplier.getPage(dataModel, option).
		done(function(data) {
			var totalCountNum = data.totalCount; //获取数据列表条数
			pageState = Math.ceil(totalCountNum / 20); //动态修改数据列表页数
			if(totalCountNum < 1) {
				if(isChioce === true) {
					$('.mui-content > .tipsConInit').html("还没有供应商资料哦!");
				}
				$('.mui-content').addClass(catalogInit);
				$('.mui-pull-top-pocket').css('display', 'none');
				mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
			} else {
				$('.mui-content').removeClass(catalogInit);
			}
			var html = template(templateId, data);
			if(dataState === true) { //判断是页面刷新还是上滑追加数据
				$(mainId).html(html);
			} else {
				$(mainId).append(html);
			}

			if(data.success) {

			} else {
				//console.log(data.error.message)

			}
		}).fail(function(data) {

		});
	}

	//		下滑刷新具体业务实现
	function pulldownRefresh() {
		pageIndex = 1;
		dataState = true;
		pageState = 10;
		setTimeout(function() {
			searchSupplier("outSupplierList", "#mui-list", "searchContentTips", false);
			mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //刷新完成
			//			mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    //隐藏已经没数据了
			mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
		}, 1500);

	}
	//		上滑加载具体业务实现
	function pullupRefresh() {
		pageIndex++;
		dataState = false;
		mui('#pullrefresh').pullRefresh().endPullupToRefresh((pageState < pageIndex)); //参数为true代表没有更多数据了。
		searchSupplier("outSupplierList", "#mui-list", "searchContentTips", false);
	}

	//首页进入标识
	if(sessionStorage.getItem("supplierHome") == "supplierHome") {
		//首页客户列表返回
		//返回清除缓存
		mui("body").on("tap", ".supplier-list-back", function() {
			commonBack.secondBack();
		});
		//ios
		dingTalkNav.LeftNavBar(commonBack.secondBack);
		//ad
		dingTalkNav.leftAndroid(commonBack.secondBack);
	} else {

		//ios
		dingTalkNav.LeftNavBar();
		//ad
		dingTalkNav.leftAndroid();
	}
});